using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using System.Utility.System;

namespace System.Utility.Logger
{
	public class SimpleLogger
	{
		public int MaxSize = 2048;

		public string LogPath = Helper.File.GetPhysicalPath("Log");

		public string FileName = "Log";

		public bool IsAsyn = true;

		private int FileIndex = 1;

		private DateTime LastDate = DateTime.Now;

		public SimpleLogger()
		{
		}

		private void AsynWriteLog(string value)
		{
			Executer.TryRunByThreadPool(() => WriteLog(value), true);
		}

		private string GetFilePath()
		{
			string filePath;
			if (DateTime.Now.Date > LastDate.Date)
			{
				FileIndex = 1;
			}
			LogPath = (LogPath.EndsWith("\\") ? LogPath.Substring(0, LogPath.Length - 1) : LogPath);
			object[] logPath = new object[] { LogPath, null, null, null };
			logPath[1] = DateTime.Now.ToString("yyyy-MM-dd");
			logPath[2] = FileName;
			logPath[3] = FileIndex;
			string str = string.Format("{0}\\{1}\\{2}{3}.log", logPath);
			if (!IO.File.Exists(str))
			{
				Helper.File.CreateFile(str, string.Empty);
			}
			if (GetFileSize(str) <= (long)MaxSize)
			{
				LastDate = DateTime.Now;
				filePath = str;
			}
			else
			{
				FileIndex++;
				filePath = GetFilePath();
			}
			return filePath;
		}

		private long GetFileSize(string filePath)
		{
			return (new FileInfo(filePath)).Length / (long)1024;
		}

		public void Log(string content, string level, Exception ex)
		{
			Write(LogFormat(content, level, ex));
		}

		private string LogFormat(string content, string logLevel, Exception exception)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append(Environment.NewLine);
			stringBuilder.AppendLine("//==========================================================================================================");
			DateTime now = DateTime.Now;
			stringBuilder.AppendFormat("系统时间：{0}", now.ToString(CultureInfo.InvariantCulture));
			stringBuilder.Append("  ");
			stringBuilder.AppendFormat("当前线程ID：{0}", Thread.CurrentThread.ManagedThreadId);
			stringBuilder.Append(Environment.NewLine);
			stringBuilder.AppendFormat("日志等级：{0}", logLevel);
			stringBuilder.Append(Environment.NewLine);
			stringBuilder.AppendFormat("日志内容：{0}", content);
			if (exception != null)
			{
				stringBuilder.Append(Environment.NewLine);
				stringBuilder.AppendLine("异常信息：");
				stringBuilder.Append(TypeExtension.ToLogString(exception));
			}
			return stringBuilder.ToString();
		}

		[Obsolete("记录消息，无任何格式处理，记录系统日志请勿使用该功能")]
		public void LogMessage(string content)
		{
			try
			{
				StreamWriter streamWriter = new StreamWriter(GetFilePath(), true);
				try
				{
					streamWriter.WriteLine(content);
					streamWriter.Flush();
				}
				finally
				{
					if (streamWriter != null)
					{
						((IDisposable)streamWriter).Dispose();
					}
				}
			}
			catch
			{
			}
		}

		private void Write(string text)
		{
			if (!IsAsyn)
			{
				WriteLog(text);
			}
			else
			{
				AsynWriteLog(text);
			}
		}

		private void WriteLog(string text)
		{
			try
			{
				StreamWriter streamWriter = new StreamWriter(GetFilePath(), true);
				try
				{
					streamWriter.WriteLine(text);
					streamWriter.Flush();
				}
				finally
				{
					if (streamWriter != null)
					{
						((IDisposable)streamWriter).Dispose();
					}
				}
			}
			catch
			{
			}
		}
	}
}